FROM node:20-alpine3.20 AS build-stage

WORKDIR /app

COPY package.json .

RUN npm config set registry https://registry.npmmirror.com/

RUN npm i -g pnpm

RUN pnpm config set registry https://registry.npmmirror.com/

RUN pnpm install

COPY . .

RUN npx prisma generate

RUN pnpm run build

# production stage
FROM node:20-alpine3.20 as production-stage

COPY --from=build-stage /app/dist /app
COPY --from=build-stage /app/start.sh /app/start.sh
COPY --from=build-stage /app/prisma /app/prisma
COPY --from=build-stage /app/.env.prod /app/.env.prod
COPY --from=build-stage /app/package.json /app/package.json
# 执行 seed.ts 时需要
COPY --from=build-stage /app/tsconfig.json /app/tsconfig.json
COPY --from=build-stage /app/tsconfig.seed.json /app/tsconfig.seed.json

WORKDIR /app

RUN npm config set registry https://registry.npmmirror.com/

RUN npm i -g cross-env

RUN npm i -g pnpm

RUN npm i -g dotenv-cli

RUN pnpm config set registry https://registry.npmmirror.com/

RUN pnpm install --production

RUN pnpm i --save-dev @types/node

EXPOSE 3000

# 移除构建时的数据库操作，改为启动时执行
CMD ["sh", "/app/start.sh"]